<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="title" label="标题"><uni-easyinput placeholder="标题" v-model="formData.title" /></uni-forms-item>
			<uni-forms-item name="body" label="消息内容"><Tinymce v-model="formData.body" :width="1000" /></uni-forms-item>
			<!-- <uni-forms-item name="published_at" label="发布时间">
				<uni-datetime-picker return-type="timestamp" :value="formData.published_at" />
				</uni-forms-item> -->
			<!-- <uni-forms-item name="updated_at" label="">
  <uni-datetime-picker return-type="timestamp" :value="formData.updated_at" />
</uni-forms-item>
<uni-forms-item name="created_at" label="">
  <uni-datetime-picker return-type="timestamp" :value="formData.created_at" />
</uni-forms-item> -->

			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;"><button class="uni-button" style="width: 100px;">返回</button></navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
import { validator } from '../../js_sdk/validator/message.js';
import Tinymce from '@/components/Tinymce';
const db = uniCloud.database();
const dbCmd = db.command;
const dbCollectionName = 'message';

function getValidator(fields) {
	let reuslt = {};
	for (let key in validator) {
		if (fields.includes(key)) {
			reuslt[key] = validator[key];
		}
	}
	return reuslt;
}

export default {
	components: {
		Tinymce
	},
	data() {
		return {
			formData: {
				title: '',
				body: '',
				published_at: null,
				updated_at: null,
				created_at: null
			},
			formOptions: {},
			rules: {
				...getValidator(['title', 'body', 'published_at', 'updated_at', 'created_at'])
			}
		};
	},
	onLoad(e) {
		const id = e.id;
		this.formDataId = id;
		this.getDetail(id);
	},
	onReady() {
		this.$refs.form.setRules(this.rules);
	},
	methods: {
		/**
		 * 触发表单提交
		 */
		submit() {
			uni.showLoading({
				mask: true
			});
			this.$refs.form
				.submit()
				.then(res => {
					this.submitForm(res);
				})
				.catch(errors => {
					uni.hideLoading();
				});
		},

		submitForm(value) {
			// 使用 clientDB 提交数据
			db.collection(dbCollectionName)
				.doc(this.formDataId)
				.update(value)
				.then(res => {
					uni.showToast({
						title: '修改成功'
					});
					this.getOpenerEventChannel().emit('refreshData');
					setTimeout(() => uni.navigateBack(), 500);
				})
				.catch(err => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					});
				})
				.finally(() => {
					uni.hideLoading();
				});
		},

		/**
		 * 获取表单数据
		 * @param {Object} id
		 */
		getDetail(id) {
			uni.showLoading({
				mask: true
			});
			db.collection(dbCollectionName)
				.doc(id)
				.field('title,body,published_at,updated_at')
				.get()
				.then(res => {
					const data = res.result.data[0];
					if (data) {
						this.formData = data;
					}
				})
				.catch(err => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					});
				})
				.finally(() => {
					uni.hideLoading();
				});
		}
	}
};
</script>
